package com.happypets.backstage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.daoImpl.PetDaoImpl;

/**
 * Servlet implementation class UpdateCategory
 */
public class UpdateCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	PetDaoImpl psi = new PetDaoImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateCategory() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		PrintWriter out = response.getWriter();

		HttpSession session = request.getSession();
		
	//   1 add b,2 add s  3,u b  4, us
		
		String flag = request.getParameter("flag");
		int t =0;
		
		System.out.println("flag:--"+flag);
		
		switch(flag){
			case "1": t = addBig(request); session.removeAttribute("lists"); break;
			case "2": t = addSmall(request); break;
			case "3": t = upBig(request);   session.removeAttribute("lists"); break;
			case "4": t = upSmall(request); break;
			case "5": t = delBig(request);break;
			case "6": t = delSmall(request);break;
		}
		
		Map<String,String> m = new HashMap<String,String>();
		
		String msg = (t == 1)?"成功":"失败";
		
		m.put("msg", msg);
		
		List<Map<String,String>> lm = new ArrayList<Map<String,String>>();
		
		lm.add(m);
		
		Gson gson=new Gson();
		String json=gson.toJson(lm);
//		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public int addBig(HttpServletRequest request){
		
		String t = request.getParameter("new-bigcategory");
		
		String sql = "insert into bigcategory(bigcategory,name) values(bigcategory_seq.nextval,'"+t+"')";
		
//		System.out.println(sql);
		
		
		
		return psi.excutQuery(sql);
	}
	
	public int addSmall(HttpServletRequest request){
		String t = request.getParameter("new-smallcategory");
		String b = request.getParameter("bigcategory");
		
		String sql = "insert into smallcategory(smallcategory,name,bigcategory) "
				+ "values(smallcategory_seq.nextval,'"+t+"',(select t.bigcategory from BIGCATEGORY t where t.name='"+b+"'))";
		System.out.println(sql);
		return psi.excutQuery(sql);
	}
	
	public int upBig(HttpServletRequest request){
		String t = request.getParameter("update-bigcategory");
		
		String o = request.getParameter("oldcategory");
		
		String sql = "update bigcategory t set t.name='"+t+"' where t.name='"+o+"'";
		System.out.println(sql);
		return psi.excutQuery(sql);
	}
	
	public int upSmall(HttpServletRequest request){
		String t = request.getParameter("update-smallcategory");
		String b = request.getParameter("bigcategory");
		String o = request.getParameter("oldcategory");
		
		String sql = "update smallcategory s set s.name='"+t+"' where s.name='"+o+"' and s.bigcategory=(select t.bigcategory from BIGCATEGORY t where t.name='"+b+"')";
//		System.out.println(sql);
		return psi.excutQuery(sql);
	}
	
	public int delBig(HttpServletRequest request){
		
		String o = request.getParameter("oldcategory");
		
		System.out.println(o);
		
		String sql = "delete bigcategory b where b.name='"+o+"'";
		System.out.println(sql);
		return psi.excutQuery(sql);
	}
	
	public int delSmall(HttpServletRequest request){
		
		String o = request.getParameter("oldcategory");
		
		String sql = "delete smallcategory a where a.name='"+o+"'";
		System.out.println(sql);
		return psi.excutQuery(sql);
	}

}
